package pentago.main;

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Scanner;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;

import org.lwjgl.LWJGLUtil;
import org.newdawn.slick.SlickException;

import pentago.engine.GameEngine;
import pentago.engine.GameOptions;

@SuppressWarnings("serial")
public class MainMenu extends JFrame {

    final private JRadioButton loadBoardRadio;

    private HelpDialog oknoPomocy = new HelpDialog();

    private JPanel contentPane;
    private JTextField firstPlayerName;
    private JTextField secondPlayerName;
    private JTextField pentagoBoardFilePath;
    private ButtonGroup firstPlayerModesGroup;
    private ButtonGroup secondPlayerModesGroup;
    private JSlider firstPlayerSlider;
    private JSlider secondPlayerSlider;
    private JCheckBox displayMovesCheckBox;
    JRadioButton secondPlayerHumanRadio;
    JRadioButton firstPlayerHumanRadio;

    private GameScreen gameScreen;
    private GameOptions gameOptions = new GameOptions();
    private Scanner scanner;
    private JTextField maximumAnswerTime;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
	System.setProperty("org.lwjgl.librarypath",
		new File(new File(System.getProperty("user.dir"), "native"),
			LWJGLUtil.getPlatformName()).getAbsolutePath());
	System.setProperty("net.java.games.input.librarypath",
		System.getProperty("org.lwjgl.librarypath"));
	EventQueue.invokeLater(new Runnable() {
	    public void run() {
		try {
		    MainMenu frame = new MainMenu();
		    frame.setVisible(true);
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	});
    }

    /**
     * Create the frame.
     */
    public MainMenu() {
	setResizable(false);
	setTitle("Pentago main menu");
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setBounds(100, 100, 583, 413);

	JMenuBar menuBar = new JMenuBar();
	setJMenuBar(menuBar);

	JMenuItem menuPomoc = new JMenuItem("Pomoc");
	menuPomoc.addMouseListener(new MouseAdapter() {
	    public void mousePressed(MouseEvent arg0) {
		oknoPomocy.setVisible(true);
		oknoPomocy.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		System.out.println("Nic");
	    }
	});
	menuPomoc.setHorizontalAlignment(SwingConstants.LEFT);
	menuBar.add(menuPomoc);
	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);
	GridBagLayout gbl_contentPane = new GridBagLayout();
	gbl_contentPane.columnWidths = new int[] { 1, 95, 117, 0, 0, 0 };
	gbl_contentPane.rowHeights = new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0 };
	gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 1.0,
		Double.MIN_VALUE };
	gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
		0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
	contentPane.setLayout(gbl_contentPane);

	JLabel label = new JLabel("");
	GridBagConstraints gbc_label = new GridBagConstraints();
	gbc_label.insets = new Insets(0, 0, 5, 5);
	gbc_label.gridx = 0;
	gbc_label.gridy = 0;
	contentPane.add(label, gbc_label);

	JLabel lblNewLabel = new JLabel("First player:");
	GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
	gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
	gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
	gbc_lblNewLabel.gridx = 1;
	gbc_lblNewLabel.gridy = 1;
	contentPane.add(lblNewLabel, gbc_lblNewLabel);

	firstPlayerName = new JTextField();
	firstPlayerName.setText("Player1");
	GridBagConstraints gbc_firstPlayerName = new GridBagConstraints();
	gbc_firstPlayerName.insets = new Insets(0, 0, 5, 5);
	gbc_firstPlayerName.fill = GridBagConstraints.HORIZONTAL;
	gbc_firstPlayerName.gridx = 2;
	gbc_firstPlayerName.gridy = 1;
	contentPane.add(firstPlayerName, gbc_firstPlayerName);
	firstPlayerName.setColumns(10);

	JLabel lblNewLabel_1 = new JLabel("Second player:");
	GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
	gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
	gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
	gbc_lblNewLabel_1.gridx = 3;
	gbc_lblNewLabel_1.gridy = 1;
	contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);

	secondPlayerName = new JTextField();
	secondPlayerName.setText("Player2");
	GridBagConstraints gbc_secondPlayerName = new GridBagConstraints();
	gbc_secondPlayerName.fill = GridBagConstraints.HORIZONTAL;
	gbc_secondPlayerName.insets = new Insets(0, 0, 5, 0);
	gbc_secondPlayerName.gridx = 4;
	gbc_secondPlayerName.gridy = 1;
	contentPane.add(secondPlayerName, gbc_secondPlayerName);
	secondPlayerName.setColumns(10);

	JLabel lblNewLabel_2 = new JLabel("First player algorithm:");
	GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
	gbc_lblNewLabel_2.anchor = GridBagConstraints.WEST;
	gbc_lblNewLabel_2.gridwidth = 2;
	gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
	gbc_lblNewLabel_2.gridx = 1;
	gbc_lblNewLabel_2.gridy = 2;
	contentPane.add(lblNewLabel_2, gbc_lblNewLabel_2);

	JLabel lblSecondPlayerAlgorithm = new JLabel("Second player algorithm:");
	GridBagConstraints gbc_lblSecondPlayerAlgorithm = new GridBagConstraints();
	gbc_lblSecondPlayerAlgorithm.insets = new Insets(0, 0, 5, 0);
	gbc_lblSecondPlayerAlgorithm.anchor = GridBagConstraints.WEST;
	gbc_lblSecondPlayerAlgorithm.gridwidth = 2;
	gbc_lblSecondPlayerAlgorithm.gridx = 3;
	gbc_lblSecondPlayerAlgorithm.gridy = 2;
	contentPane.add(lblSecondPlayerAlgorithm, gbc_lblSecondPlayerAlgorithm);

	firstPlayerHumanRadio = new JRadioButton("Human");
	firstPlayerHumanRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		gameOptions.setFirstPlayerMode(GameOptions.HUMAN_PLAYER);
		firstPlayerSlider.setEnabled(false);
		displayMovesCheckBox.setEnabled(false);
		displayMovesCheckBox.setSelected(true);
	    }
	});
	firstPlayerHumanRadio.setSelected(true);
	GridBagConstraints gbc_firstPlayerHumanRadio = new GridBagConstraints();
	gbc_firstPlayerHumanRadio.anchor = GridBagConstraints.WEST;
	gbc_firstPlayerHumanRadio.insets = new Insets(0, 0, 5, 5);
	gbc_firstPlayerHumanRadio.gridx = 1;
	gbc_firstPlayerHumanRadio.gridy = 3;
	contentPane.add(firstPlayerHumanRadio, gbc_firstPlayerHumanRadio);

	secondPlayerHumanRadio = new JRadioButton("Human");
	secondPlayerHumanRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		gameOptions.setSecondPlayerMode(GameOptions.HUMAN_PLAYER);
		secondPlayerSlider.setEnabled(false);
		displayMovesCheckBox.setEnabled(false);
		displayMovesCheckBox.setSelected(true);
	    }
	});
	secondPlayerHumanRadio.setSelected(true);
	GridBagConstraints gbc_secondPlayerHumanRadio = new GridBagConstraints();
	gbc_secondPlayerHumanRadio.anchor = GridBagConstraints.WEST;
	gbc_secondPlayerHumanRadio.insets = new Insets(0, 0, 5, 5);
	gbc_secondPlayerHumanRadio.gridx = 3;
	gbc_secondPlayerHumanRadio.gridy = 3;
	contentPane.add(secondPlayerHumanRadio, gbc_secondPlayerHumanRadio);

	JRadioButton firstPlayerRandomRadio = new JRadioButton("Random");
	firstPlayerRandomRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		gameOptions.setFirstPlayerMode(GameOptions.COMPUTER_RANDOM);
		firstPlayerSlider.setEnabled(false);
		if (!secondPlayerHumanRadio.isSelected())
		    displayMovesCheckBox.setEnabled(true);
	    }
	});
	GridBagConstraints gbc_firstPlayerRandomRadio = new GridBagConstraints();
	gbc_firstPlayerRandomRadio.anchor = GridBagConstraints.WEST;
	gbc_firstPlayerRandomRadio.insets = new Insets(0, 0, 5, 5);
	gbc_firstPlayerRandomRadio.gridx = 1;
	gbc_firstPlayerRandomRadio.gridy = 4;
	contentPane.add(firstPlayerRandomRadio, gbc_firstPlayerRandomRadio);

	JRadioButton secondPlayerRandomRadio = new JRadioButton("Random");
	secondPlayerRandomRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		gameOptions.setSecondPlayerMode(GameOptions.COMPUTER_RANDOM);
		secondPlayerSlider.setEnabled(false);
		if (!firstPlayerHumanRadio.isSelected())
		    displayMovesCheckBox.setEnabled(true);
	    }
	});
	GridBagConstraints gbc_secondPlayerRandomRadio = new GridBagConstraints();
	gbc_secondPlayerRandomRadio.anchor = GridBagConstraints.WEST;
	gbc_secondPlayerRandomRadio.insets = new Insets(0, 0, 5, 5);
	gbc_secondPlayerRandomRadio.gridx = 3;
	gbc_secondPlayerRandomRadio.gridy = 4;
	contentPane.add(secondPlayerRandomRadio, gbc_secondPlayerRandomRadio);

	JRadioButton firstPlayerAlphaBetaRadio = new JRadioButton("Alpha-Beta");
	firstPlayerAlphaBetaRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		gameOptions.setFirstPlayerMode(GameOptions.COMPUTER_ALPHA_BETA);
		firstPlayerSlider.setEnabled(true);
		if (!secondPlayerHumanRadio.isSelected())
		    displayMovesCheckBox.setEnabled(true);
	    }
	});
	GridBagConstraints gbc_firstPlayerAlphaBetaRadio = new GridBagConstraints();
	gbc_firstPlayerAlphaBetaRadio.anchor = GridBagConstraints.WEST;
	gbc_firstPlayerAlphaBetaRadio.insets = new Insets(0, 0, 5, 5);
	gbc_firstPlayerAlphaBetaRadio.gridx = 1;
	gbc_firstPlayerAlphaBetaRadio.gridy = 5;
	contentPane.add(firstPlayerAlphaBetaRadio,
		gbc_firstPlayerAlphaBetaRadio);

	firstPlayerSlider = new JSlider();
	firstPlayerSlider.setToolTipText("Algorithm search depth");
	firstPlayerSlider.setPaintTrack(false);
	firstPlayerSlider.setEnabled(false);
	firstPlayerSlider.setSnapToTicks(true);
	firstPlayerSlider.setMajorTickSpacing(1);
	firstPlayerSlider.setMinorTickSpacing(1);
	firstPlayerSlider.setPaintTicks(true);
	firstPlayerSlider.setPaintLabels(true);
	firstPlayerSlider.setValue(0);
	firstPlayerSlider.setMaximum(5);
	GridBagConstraints gbc_firstPlayerSlider = new GridBagConstraints();
	gbc_firstPlayerSlider.fill = GridBagConstraints.HORIZONTAL;
	gbc_firstPlayerSlider.gridheight = 2;
	gbc_firstPlayerSlider.insets = new Insets(0, 0, 5, 5);
	gbc_firstPlayerSlider.gridx = 2;
	gbc_firstPlayerSlider.gridy = 5;
	contentPane.add(firstPlayerSlider, gbc_firstPlayerSlider);

	JRadioButton secondPlayerAlphaBetaRadio = new JRadioButton("Alpha-Beta");
	secondPlayerAlphaBetaRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		gameOptions
			.setSecondPlayerMode(GameOptions.COMPUTER_ALPHA_BETA);
		secondPlayerSlider.setEnabled(true);
		if (!firstPlayerHumanRadio.isSelected())
		    displayMovesCheckBox.setEnabled(true);
	    }
	});
	GridBagConstraints gbc_secondPlayerAlphaBetaRadio = new GridBagConstraints();
	gbc_secondPlayerAlphaBetaRadio.anchor = GridBagConstraints.WEST;
	gbc_secondPlayerAlphaBetaRadio.insets = new Insets(0, 0, 5, 5);
	gbc_secondPlayerAlphaBetaRadio.gridx = 3;
	gbc_secondPlayerAlphaBetaRadio.gridy = 5;
	contentPane.add(secondPlayerAlphaBetaRadio,
		gbc_secondPlayerAlphaBetaRadio);

	secondPlayerSlider = new JSlider();
	secondPlayerSlider.setToolTipText("Algorithm search depth");
	secondPlayerSlider.setPaintTrack(false);
	secondPlayerSlider.setEnabled(false);
	secondPlayerSlider.setMajorTickSpacing(1);
	secondPlayerSlider.setSnapToTicks(true);
	secondPlayerSlider.setMaximum(5);
	secondPlayerSlider.setValue(0);
	secondPlayerSlider.setPaintTicks(true);
	secondPlayerSlider.setPaintLabels(true);
	secondPlayerSlider.setMinorTickSpacing(1);
	GridBagConstraints gbc_secondPlayerSlider = new GridBagConstraints();
	gbc_secondPlayerSlider.fill = GridBagConstraints.HORIZONTAL;
	gbc_secondPlayerSlider.gridheight = 2;
	gbc_secondPlayerSlider.insets = new Insets(0, 0, 5, 0);
	gbc_secondPlayerSlider.gridx = 4;
	gbc_secondPlayerSlider.gridy = 5;
	contentPane.add(secondPlayerSlider, gbc_secondPlayerSlider);

	JRadioButton firstPlayerOtherRadio = new JRadioButton(
		"Transposition Table");
	firstPlayerOtherRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		gameOptions.setFirstPlayerMode(GameOptions.COMPUTER_OTHER);
		firstPlayerSlider.setEnabled(true);
		if (!secondPlayerHumanRadio.isSelected())
		    displayMovesCheckBox.setEnabled(true);
	    }
	});
	GridBagConstraints gbc_firstPlayerOtherRadio = new GridBagConstraints();
	gbc_firstPlayerOtherRadio.anchor = GridBagConstraints.WEST;
	gbc_firstPlayerOtherRadio.insets = new Insets(0, 0, 5, 5);
	gbc_firstPlayerOtherRadio.gridx = 1;
	gbc_firstPlayerOtherRadio.gridy = 6;
	contentPane.add(firstPlayerOtherRadio, gbc_firstPlayerOtherRadio);

	JRadioButton secondPlayerOtherRadio = new JRadioButton(
		"Transposition Table");
	secondPlayerOtherRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		gameOptions.setSecondPlayerMode(GameOptions.COMPUTER_OTHER);
		secondPlayerSlider.setEnabled(true);
		if (!firstPlayerHumanRadio.isSelected())
		    displayMovesCheckBox.setEnabled(true);
	    }
	});
	GridBagConstraints gbc_secondPlayerOtherRadio = new GridBagConstraints();
	gbc_secondPlayerOtherRadio.anchor = GridBagConstraints.WEST;
	gbc_secondPlayerOtherRadio.insets = new Insets(0, 0, 5, 5);
	gbc_secondPlayerOtherRadio.gridx = 3;
	gbc_secondPlayerOtherRadio.gridy = 6;
	contentPane.add(secondPlayerOtherRadio, gbc_secondPlayerOtherRadio);

	firstPlayerModesGroup = new ButtonGroup();
	firstPlayerModesGroup.add(firstPlayerHumanRadio);
	firstPlayerModesGroup.add(firstPlayerRandomRadio);
	firstPlayerModesGroup.add(firstPlayerAlphaBetaRadio);
	firstPlayerModesGroup.add(firstPlayerOtherRadio);

	secondPlayerModesGroup = new ButtonGroup();
	secondPlayerModesGroup.add(secondPlayerHumanRadio);
	secondPlayerModesGroup.add(secondPlayerRandomRadio);
	secondPlayerModesGroup.add(secondPlayerAlphaBetaRadio);
	secondPlayerModesGroup.add(secondPlayerOtherRadio);

	final JButton firstPlayerColorButton = new JButton("First player color");
	firstPlayerColorButton.setBackground(Color.white);
	firstPlayerColorButton.putClientProperty("parent", contentPane);
	firstPlayerColorButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		JComponent c = (JComponent) e.getSource();
		Color chosenColor = JColorChooser.showDialog(
			(Component) c.getClientProperty("parent"),
			"First player color selection", Color.black);
		if (chosenColor != null) {
		    firstPlayerColorButton.setBackground(chosenColor);
		    gameOptions
			    .setFirstPlayerColor(changeAWT2SlickColor(chosenColor));
		}
	    }
	});
	GridBagConstraints gbc_firstPlayerColorButton = new GridBagConstraints();
	gbc_firstPlayerColorButton.fill = GridBagConstraints.HORIZONTAL;
	gbc_firstPlayerColorButton.gridwidth = 2;
	gbc_firstPlayerColorButton.insets = new Insets(0, 0, 5, 5);
	gbc_firstPlayerColorButton.gridx = 1;
	gbc_firstPlayerColorButton.gridy = 7;
	contentPane.add(firstPlayerColorButton, gbc_firstPlayerColorButton);

	final JButton secondPlayerColorButton = new JButton(
		"Second player color");
	secondPlayerColorButton.setBackground(Color.black);
	secondPlayerColorButton.putClientProperty("parent", contentPane);
	secondPlayerColorButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		JComponent c = (JComponent) e.getSource();
		Color chosenColor = JColorChooser.showDialog(
			(Component) c.getClientProperty("parent"),
			"Second player color selection", Color.black);
		if (chosenColor != null) {
		    secondPlayerColorButton.setBackground(chosenColor);
		    gameOptions
			    .setSecondPlayerColor(changeAWT2SlickColor(chosenColor));
		}
	    }
	});
	GridBagConstraints gbc_secondPlayerColorButton = new GridBagConstraints();
	gbc_secondPlayerColorButton.insets = new Insets(0, 0, 5, 0);
	gbc_secondPlayerColorButton.gridwidth = 2;
	gbc_secondPlayerColorButton.fill = GridBagConstraints.HORIZONTAL;
	gbc_secondPlayerColorButton.gridx = 3;
	gbc_secondPlayerColorButton.gridy = 7;
	contentPane.add(secondPlayerColorButton, gbc_secondPlayerColorButton);

	final JRadioButton cleanBoardRadio = new JRadioButton("Clean board");
	cleanBoardRadio.setSelected(true);
	cleanBoardRadio.putClientProperty("parent", contentPane);
	cleanBoardRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		pentagoBoardFilePath.setText("");
	    }
	});

	final JButton plateColorButton = new JButton("Plate color");
	plateColorButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		JComponent c = (JComponent) e.getSource();
		Color chosenColor = JColorChooser.showDialog(
			(Component) c.getClientProperty("parent"),
			"Plate color selection", Color.red);
		if (chosenColor != null) {
		    plateColorButton.setBackground(chosenColor);
		    gameOptions
			    .setPlateColor(changeAWT2SlickColor(chosenColor));
		}
	    }
	});
	plateColorButton.setBackground(Color.RED);
	GridBagConstraints gbc_plateColorButton = new GridBagConstraints();
	gbc_plateColorButton.fill = GridBagConstraints.HORIZONTAL;
	gbc_plateColorButton.gridwidth = 4;
	gbc_plateColorButton.insets = new Insets(0, 0, 5, 0);
	gbc_plateColorButton.gridx = 1;
	gbc_plateColorButton.gridy = 8;
	contentPane.add(plateColorButton, gbc_plateColorButton);
	GridBagConstraints gbc_cleanBoardRadio = new GridBagConstraints();
	gbc_cleanBoardRadio.anchor = GridBagConstraints.WEST;
	gbc_cleanBoardRadio.insets = new Insets(0, 0, 5, 5);
	gbc_cleanBoardRadio.gridx = 1;
	gbc_cleanBoardRadio.gridy = 9;
	contentPane.add(cleanBoardRadio, gbc_cleanBoardRadio);

	loadBoardRadio = new JRadioButton("From file:");
	loadBoardRadio.putClientProperty("parent", contentPane);
	loadBoardRadio.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		JFileChooser fc = new JFileChooser();
		fc.setFileFilter(new FileFilter() {

		    @Override
		    public String getDescription() {
			return "Pentago board files (.pbr)";
		    }

		    @Override
		    public boolean accept(File file) {
			if (file.isDirectory())
			    return true;
			if (file.isFile()) {
			    return file.getPath().endsWith(".pbr");
			}
			return false;
		    }
		});
		fc.setAcceptAllFileFilterUsed(false);
		JComponent c = (JComponent) e.getSource();
		int returnVal = fc.showOpenDialog((Component) c
			.getClientProperty("parent"));
		if (returnVal == JFileChooser.APPROVE_OPTION) {
		    File file = fc.getSelectedFile();
		    pentagoBoardFilePath.setText(file.getPath());
		} else {
		    cleanBoardRadio.setSelected(true);
		}
	    }
	});
	GridBagConstraints gbc_loadBoardRadio = new GridBagConstraints();
	gbc_loadBoardRadio.anchor = GridBagConstraints.WEST;
	gbc_loadBoardRadio.insets = new Insets(0, 0, 5, 5);
	gbc_loadBoardRadio.gridx = 1;
	gbc_loadBoardRadio.gridy = 10;
	contentPane.add(loadBoardRadio, gbc_loadBoardRadio);

	ButtonGroup boardSourceGroup = new ButtonGroup();
	boardSourceGroup.add(cleanBoardRadio);
	boardSourceGroup.add(loadBoardRadio);

	pentagoBoardFilePath = new JTextField();
	pentagoBoardFilePath.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mousePressed(MouseEvent e) {
		loadBoardRadio.setSelected(true);
	    }
	});

	GridBagConstraints gbc_pentagoBoardFilePath = new GridBagConstraints();
	gbc_pentagoBoardFilePath.gridwidth = 3;
	gbc_pentagoBoardFilePath.insets = new Insets(0, 0, 5, 0);
	gbc_pentagoBoardFilePath.fill = GridBagConstraints.HORIZONTAL;
	gbc_pentagoBoardFilePath.gridx = 2;
	gbc_pentagoBoardFilePath.gridy = 10;
	contentPane.add(pentagoBoardFilePath, gbc_pentagoBoardFilePath);
	pentagoBoardFilePath.setColumns(10);

	JButton startGameButton = new JButton("Start");
	startGameButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		if (!setBoardState())
		    return;
		String timeLimit = maximumAnswerTime.getText();
		if (!stringCorrectLong(timeLimit)) {
		    JOptionPane.showMessageDialog(null,
			    "Time limit must be numeric.");
		    return;
		}
		Long maxRunTime = Long.parseLong(timeLimit);
		if (maxRunTime <= 0) {
		    gameOptions.setMaxRunTime(Long.MAX_VALUE);
		} else {
		    gameOptions.setMaxRunTime(Long.parseLong(timeLimit) * 1000);
		}
		gameOptions.setFirstPlayerName(firstPlayerName.getText());
		gameOptions.setSecondPlayerName(secondPlayerName.getText());
		gameOptions.setFirstPlayerDepth((byte) firstPlayerSlider
			.getValue());
		gameOptions.setSecondPlayerDepth((byte) secondPlayerSlider
			.getValue());
		gameOptions.setDisplayMoves(displayMovesCheckBox.isSelected());
		gameScreen = new GameScreen();
		try {
		    gameScreen.startGame(gameOptions);
		} catch (SlickException e1) {
		    e1.printStackTrace();
		}
	    }
	});
	GridBagConstraints gbc_startGameButton = new GridBagConstraints();
	gbc_startGameButton.gridheight = 2;
	gbc_startGameButton.insets = new Insets(0, 0, 5, 0);
	gbc_startGameButton.gridwidth = 4;
	gbc_startGameButton.gridx = 1;
	gbc_startGameButton.gridy = 11;
	contentPane.add(startGameButton, gbc_startGameButton);

	JLabel maximumAnserTimeLabel = new JLabel("Maximum answer time:");
	GridBagConstraints gbc_maximumAnserTimeLabel = new GridBagConstraints();
	gbc_maximumAnserTimeLabel.insets = new Insets(0, 0, 0, 5);
	gbc_maximumAnserTimeLabel.anchor = GridBagConstraints.WEST;
	gbc_maximumAnserTimeLabel.gridx = 1;
	gbc_maximumAnserTimeLabel.gridy = 13;
	contentPane.add(maximumAnserTimeLabel, gbc_maximumAnserTimeLabel);

	maximumAnswerTime = new JTextField();
	maximumAnswerTime.setHorizontalAlignment(SwingConstants.RIGHT);
	maximumAnswerTime.setText("0");
	GridBagConstraints gbc_maximumAnswerTime = new GridBagConstraints();
	gbc_maximumAnswerTime.insets = new Insets(0, 0, 0, 5);
	gbc_maximumAnswerTime.fill = GridBagConstraints.HORIZONTAL;
	gbc_maximumAnswerTime.gridx = 2;
	gbc_maximumAnswerTime.gridy = 13;
	contentPane.add(maximumAnswerTime, gbc_maximumAnswerTime);
	maximumAnswerTime.setColumns(10);

	JLabel maximumAnserTimeUnit = new JLabel("[s] (<=0 unlimited)");
	GridBagConstraints gbc_maximumAnserTimeUnit = new GridBagConstraints();
	gbc_maximumAnserTimeUnit.anchor = GridBagConstraints.WEST;
	gbc_maximumAnserTimeUnit.insets = new Insets(0, 0, 0, 5);
	gbc_maximumAnserTimeUnit.gridx = 3;
	gbc_maximumAnserTimeUnit.gridy = 13;
	contentPane.add(maximumAnserTimeUnit, gbc_maximumAnserTimeUnit);

	displayMovesCheckBox = new JCheckBox("Display moves");
	displayMovesCheckBox.setEnabled(false);
	displayMovesCheckBox.setSelected(true);
	GridBagConstraints gbc_displayMovesCheckBox = new GridBagConstraints();
	gbc_displayMovesCheckBox.anchor = GridBagConstraints.EAST;
	gbc_displayMovesCheckBox.gridx = 4;
	gbc_displayMovesCheckBox.gridy = 13;
	contentPane.add(displayMovesCheckBox, gbc_displayMovesCheckBox);
    }

    private boolean setBoardState() {

	byte[][] board = new byte[6][6];
	if (loadBoardRadio.isSelected()) {
	    if (!readBoardFile(pentagoBoardFilePath.getText(), board)) {
		JOptionPane.showMessageDialog(this,
			"Selected file is not a valid Pentago board file.");
		return false;
	    }
	} else {
	    for (int i = 0; i < 6; i++)
		for (int j = 0; j < 6; j++)
		    board[i][j] = GameEngine.NO_PLAYER;
	}
	gameOptions.setGameBoard(board);

	return true;
    }

    // private byte getPlayerMode(Enumeration<AbstractButton> radioButtonsGroup,
    // byte player) {
    // JRadioButton modeButton;
    // while (radioButtonsGroup.hasMoreElements()) {
    // modeButton = (JRadioButton) radioButtonsGroup.nextElement();
    // if (modeButton.isSelected()) {
    // return;
    // }
    // }
    // return -1;
    // }

    private boolean readBoardFile(String path, byte[][] board) {
	int i, j;
	int firstPlayerCount, secondPlayerCount;
	File file;
	try {
	    i = j = 0;
	    firstPlayerCount = secondPlayerCount = 0;
	    file = new File(path);
	    if (!file.isFile())
		return false;
	    scanner = new Scanner(file);
	    String line;
	    while (scanner.hasNext()) {
		line = scanner.next();
		if (line.length() != 6)
		    return false;
		j = 0;
		for (byte field : line.getBytes()) {
		    switch (field - 48) {
		    case GameEngine.NO_PLAYER:
			board[j][i] = GameEngine.NO_PLAYER;
			break;
		    case GameEngine.FIRST_PLAYER:
			board[j][i] = GameEngine.FIRST_PLAYER;
			++firstPlayerCount;
			break;
		    case GameEngine.SECOND_PLAYER:
			board[j][i] = GameEngine.SECOND_PLAYER;
			++secondPlayerCount;
			break;
		    default:
			return false;
		    }
		    ++j;
		}
		++i;
	    }
	} catch (Exception e) {
	    return false;
	} finally {
	    if (scanner != null)
		scanner.close();
	    scanner = null;
	}
	return i == j
		&& j == 6
		&& (firstPlayerCount - secondPlayerCount == 0 || firstPlayerCount
			- secondPlayerCount == 1);
    }

    private org.newdawn.slick.Color changeAWT2SlickColor(Color color) {
	return new org.newdawn.slick.Color(color.getRGB());
    }

    private boolean stringCorrectLong(String string) {
	try {
	    Long.parseLong(string);
	} catch (NumberFormatException nfe) {
	    return false;
	}
	return true;
    }
}
